#include <bits/stdc++.h>
using namespace std;
#define int long long
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

void solve() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    vector<vector<int>> memo(n, vector<int>(m));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) cin >> a[i][j];
    }
    int ans = 0;
    auto dfs = [&](auto&& dfs, int x, int y)->int {
        int res = a[x][y];
        memo[x][y] = 1;
        for(int i = 0; i < 4; i++) {
            int tx = dx[i] + x, ty = dy[i] + y;
            if(tx >= 0 && tx < n && ty >= 0 && ty < m && a[tx][ty] && !memo[tx][ty]) {
                res += dfs(dfs, tx, ty);
            }
        }
        return res;
    };
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(memo[i][j] == 1 || a[i][j] == 0) continue;
            ans = max(ans, dfs(dfs, i, j));
        }
    }
    cout << ans << endl;
}

signed main() {
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}